//https://leetcode.cn/problems/daily-temperatures/
package codeRandomThoughts.Test739每日温度;

import java.util.Stack;

//单调栈
//用栈实现
public class Solution2 {
    public int[] dailyTemperatures(int[] temperatures) {
        //单调栈 从栈顶到栈底单调递增 栈内部存的是元素的下标
        Stack<Integer> stack = new Stack<>();

        int[] result = new int[temperatures.length];

        //遍历整个temperatures
        for (int i = 0; i < temperatures.length; i++) {
            while (!stack.isEmpty() && temperatures[stack.peek()] < temperatures[i]) {
                Integer pop = stack.pop();
                result[pop] = i - pop;
            }

            stack.push(i);
        }

        return result;
    }
}
